package com.mz.ozy.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.bean.CarGoodsBean;
import com.mz.ozy.bean.CarGoodsDetailBean;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * Created by Administrator on 2018/11/21.
 */

public class ReturnGoodsListAdapter extends BaseQuickAdapter<CarGoodsBean.PageBean.PageItemsBean, BaseViewHolder> {

    public ReturnGoodsListAdapter(@Nullable List<CarGoodsBean.PageBean.PageItemsBean> data) {
        super(R.layout.view_item_return_goods_list, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CarGoodsBean.PageBean.PageItemsBean item) {

        helper.addOnClickListener(R.id.tv_details);
        helper.addOnClickListener(R.id.ll_item_view);
        helper.setText(R.id.tv_time, item.getCreated());
        helper.setText(R.id.tv_title, item.getProduct_name());
        helper.setText(R.id.tv_info, item.getArrribute_name());
        helper.setText(R.id.tv_money, getPrice(item));
        helper.setText(R.id.tv_count, "x" + item.getNum());
        if (item.getState() == 0) {
            helper.setText(R.id.tv_state, "待商家确认");
            helper.setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.c_fbcc14));
        } else if (item.getState() == 1) {
            helper.setText(R.id.tv_state, "待买家发货");
            helper.setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.c_fbcc14));

        } else if (item.getState() == 2) {
            helper.setText(R.id.tv_state, "待商家收货");
            helper.setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.c_fbcc14));

        } else if (item.getState() == 3) {
            helper.setText(R.id.tv_state, "退款成功");
            helper.setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.c_363636));

        } else if (item.getState() == 4) {
            helper.setText(R.id.tv_state, "待平台退款");
            helper.setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.c_fbcc14));

        } else {
            helper.setText(R.id.tv_state, "售后关闭");
            helper.setTextColor(R.id.tv_state, mContext.getResources().getColor(R.color.c_363636));

        }
        TextView tvRemainTime = helper.getView(R.id.tv_remaining_time);
        startCountDown(item, tvRemainTime);

        helper.setText(R.id.tv_time, item.getCreated());
        ImageView imageView = helper.getView(R.id.iv_icon);

        String img_zt = "";
        if (!TextUtils.isEmpty(item.getZtimg())) {
            final String[] splitImgZt = item.getZtimg().split(",");
            if (splitImgZt.length > 0) {
                img_zt = splitImgZt[0];
            }
        }
        if (!img_zt.equals(imageView.getTag())) {
            imageView.setTag(img_zt);
            GlideUtils.loadRoundImg(mContext, imageView, img_zt, R.mipmap.default_foods_icon, 4);
        }
    }

    private String getPrice(CarGoodsBean.PageBean.PageItemsBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getAmount_zt()) && Double.parseDouble(item.getAmount_zt()) != 0.00) {
            price += "¥";
            price += Utils.decimalUtils(item.getAmount_zt(), 2);
        }
        if (!TextUtils.isEmpty(item.getAmount_xf()) && Double.parseDouble(item.getAmount_xf()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += Utils.decimalUtils(item.getAmount_xf(), 2);
                price += "积分";
            } else {
                price += Utils.decimalUtils(item.getAmount_xf(), 2);
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }

        if(item.getProducttype() == 1){
            return price.replace("¥", "") + "欧花";
        }

        return price;
    }

    private void startCountDown(CarGoodsBean.PageBean.PageItemsBean item, TextView view) {
        long second = item.getSpaceTime();
        long minute = 0;
        long hour = 0;
        long day = 0;
        if (second >= 60) {

            minute = second / 60;
            second = second % 60;
            if (minute >= 60) {

                hour = minute / 60;
                minute = minute % 60;
                if (hour > 24) {

                    day = hour / 24;
                    hour = hour % 24;
                }
            }
        }
        if (item.getState() != 0) {
            view.setVisibility(View.GONE);
            view.setText(day + "天" + second);
        } else {
            view.setVisibility(View.VISIBLE);
            view.setText("还剩" + day + "天" + Utils.formatHMS(hour, minute, second));
        }

    }
}
